<?php

class Home extends M_Controller
{

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页
     */
    public function index()
    {
        parent::_index();
    }

    // 创作者列表
    public function issuerList()
    {
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['pageSize'] ?: 10;
        $keyword = $this->input->get('keyword', true);
        $issuerList = $this->models('issuer')->issuerList('id,title,thumb,description', $page, $pageSize, $keyword);
        $end = $pageSize > count($issuerList);
        if ($issuerList) {
            $creator = array_column($issuerList, 'id');
            $creatorList = $this->models('issuer')->creatorWorks($creator);
            foreach ($creatorList as &$item) {
                $item['thumb'] = dr_thumb($item['thumb'], 150, 150);
                foreach ($issuerList as &$issuer) {
                    $issuer['thumb'] = dr_thumb($issuer['thumb'], 550, 550);
                    if ($item['creator'] == $issuer['id']) {
                        $issuer['works'][] = $item;
                    }
                }
            }
        }
        apiSuccess(1, 'success', ['list' => $issuerList, 'end' => $end]);
    }

    // 创作者详情
    public function issuerInfo()
    {
        $id = $this->input->get('id');
        if ((int)$id < 1) {
            apiSuccess(0, '参数错误');
        }
        $info = $this->models('issuer')->issuerInfo($id);
        if (empty($info)) {
            apiSuccess(0, '内容不存在');
        }
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['pageSize'] ?: 10;
        $goods = $this->models('favorite')->favoriteList(
            'id,title,thumb,price,nums,buy_nums,creator',
            $page,
            $pageSize,
            ['creator' => $info['id']]
        );
        apiSuccess(1, 'success', [
            'info' => $this->models('issuer')->issuerTransfer($info),
            'list' => array_map(function (&$item) {
                return $this->models('favorite')->favoriteTransfer($item);
            }, $goods),
            'end' => $pageSize > count($goods)
        ]);
    }
}